<?php

class M_config extends CI_Model {

    public $temp_node = array();
    public $temp_tree = array();

    function __construct() {
        parent::__construct();
        $this->load->database();
    }
    function get_count($data) {
        $this->db->from('config');
        $this->db->select('count(1) as cnt');
        $this->db->where($data);
        $result = $this->db->get()->row_array();
        return $result['cnt'];
    }

    function get_all($data=array()){
        if (isset($data['column'])) {
            $this->db->select($data['column']);
        }
        if (isset($data['where'])) {
            $this->db->select($data['where']);
        }
        $result=array();
        $query = $this->db->get('config');
        foreach ($query->result_array() as $row) {
            $result[$row['configid']] = $row;
        }
        return $result;
    }

    function get_admin_log($where, $num = '', $offset = '') {
        $this->db->select('adminlog.*,admin.adminname');
        $this->db->from('adminlog');
        $this->db->order_by('adminlog.adminid', 'DESC');
        $this->db->join('admin', 'adminlog.adminid=admin.adminid', 'left');
        $this->db->where($where);
        if ($num) {
            $this->db->limit($num, $offset);
        }
        $query = $this->db->get(); //echo $this->db->last_query();
        return $query->result_array();
    }

    function get_admingroup($where, $num = '', $offset = '') {
        $this->db->select('admingroup.*,admin.adminname');
        $this->db->from('admingroup');
        $this->db->join('admin', 'admingroup.adminid=admin.adminid', 'left');
        $this->db->where($where);
        if ($num) {
            $this->db->limit($num, $offset);
        }
        $query = $this->db->get(); //echo $this->db->last_query();
        return $query->result_array();
    }

    function get_adminingroup($where, $num = '', $offset = '') {
        $this->db->select('adminingroup.*,admin.adminname');
        $this->db->from('adminingroup');
        $this->db->join('admin', 'adminingroup.adminid=admin.adminid', 'left');
        $this->db->where($where);
        if ($num) {
            $this->db->limit($num, $offset);
        }
        $query = $this->db->get(); //echo $this->db->last_query();
        return $query->result_array();
    }

    function get_limit($where, $num = '', $offset = '') {
        $this->db->select('limit.*,admin.adminname');
        $this->db->from('limit');
        $this->db->join('admin', 'limit.adminid=admin.adminid', 'left');
        $this->db->where($where);
        if ($num) {
            $this->db->limit($num, $offset);
        }
        $query = $this->db->get(); //echo $this->db->last_query();
        return $query->result_array();
    }

    function get_limit_mv($data){
        $this->db->from('sysmodule');
        if (isset($data['limit'])) {
            $this->db->limit($data['limit'], $data['offset']);
        }
        if (isset($data['where'])) {
            $this->db->where($data['where']);
        }
        if (isset($data['order'])) {
            $this->db->order_by($data['order']);
        }
        if (isset($data['column'])) {
            $this->db->select($data['column']);
        }
        $query = $this->db->get();
        return $query->result_array();
    }

    function get_node($classid) {
        if ($classid == 0) {
            return $this->temp_node;
        }

        if (count($this->temp_node) > 5) {
            return $this->temp_node;
        }
        $this->db->select('limitfield,parentid,limitid');
        $result = $this->db->get_where('limit', array('limitid' => $classid))->row_array();

        if ($result['parentid'] != 0) {
            $this->get_node($result['parentid']);
        }

        $this->temp_node[] = $result;

        return $this->temp_node;
    }

    function get_module_node($classid) {
        if ($classid == 0) {
            return $this->temp_node;
        }

        if (count($this->temp_node) > 5) {
            return $this->temp_node;
        }
        $this->db->select('sysmoduleid,parentid,modulename');
        $result = $this->db->get_where('sysmodule', array('sysmoduleid' => $classid))->row_array();

        if ($result['parentid'] != 0) {
            $this->get_module_node($result['parentid']);
        }

        $this->temp_node[] = $result;

        return $this->temp_node;
    }

    function get_sysrole($where, $num = '', $offset = '') {
        $this->db->select('sysrole.*,admin.adminname,sysrolegroup.rolegroupname');
        $this->db->from('sysrole');
        $this->db->join('admin', 'sysrole.adminid=admin.adminid', 'left');
        $this->db->join('sysrolegroup', 'sysrole.sysrolegroupid=sysrolegroup.sysrolegroupid', 'left');
        $this->db->where($where);
        if ($num) {
            $this->db->limit($num, $offset);
        }
        $query = $this->db->get(); //echo $this->db->last_query();
        return $query->result_array();
    }

    function get_sysrolegroup($where, $num = '', $offset = '') {
        $this->db->select('sysrolegroup.*,admin.adminname');
        $this->db->from('sysrolegroup');
        $this->db->join('admin', 'sysrolegroup.adminid=admin.adminid', 'left');
        $this->db->where($where);
        if ($num) {
            $this->db->limit($num, $offset);
        }
        $query = $this->db->get(); //echo $this->db->last_query();
        return $query->result_array();
    }

    function get_area($where) {
        $this->db->select('area.*,admin.adminname');
        $this->db->from('area');
        $this->db->join('admin', 'area.adminid=admin.adminid', 'left');
        $this->db->where($where);
        $query = $this->db->get(); //echo $this->db->last_query();
        return $query->result_array();
    }

    function get_syslimit($where, $num = '', $offset = '') {
        $this->db->select('syslimit.*,admin.adminname');
        $this->db->from('syslimit');
        $this->db->join('admin', 'syslimit.adminid=admin.adminid', 'left');
        $this->db->where($where);
        if ($num) {
            $this->db->limit($num, $offset);
        }
        $query = $this->db->get(); //echo $this->db->last_query();
        return $query->result_array();
    }

    function get_sysmodule($where, $num = '', $offset = '') {
        $this->db->select('sysmodule.*,admin.adminname');
        $this->db->from('sysmodule');
        $this->db->join('admin', 'sysmodule.adminid=admin.adminid', 'left');
        $this->db->where($where);
        if ($num) {
            $this->db->limit($num, $offset);
        }
        $query = $this->db->get(); //echo $this->db->last_query();
        return $query->result_array();
    }

    function get_config($where, $num = '', $offset = '') {
        $this->db->select('config.*,admin.adminname');
        $this->db->from('config');
        $this->db->join('admin', 'config.adminid=admin.adminid', 'left');
        $this->db->where($where);
        if ($num) {
            $this->db->limit($num, $offset);
        }
        $query = $this->db->get(); //echo $this->db->last_query();
        return $query->result_array();
    }


}
